function mapCourseTypeLabel(courseType) {
  switch (courseType) {
    case "1":
      return "私教课";
    case "2":
      return "团体课";
    case "3":
      return "班课";
    default:
      return "-";
  }
}

function transformData(originalData) {
  const targetData = {
    listVenues: [],
  };

  const venueMap = {};

  originalData.forEach((item) => {
    if (!venueMap[item.venueId]) {
      venueMap[item.venueId] = {
        value: item.venueId,
        label: item.venueName,
        children: [],
      };
      targetData.listVenues.push(venueMap[item.venueId]);
    }

    const venue = venueMap[item.venueId];
    const courseTypeIndex = venue.children.findIndex(
      (child) => child.value === item.courseType
    );

    if (courseTypeIndex === -1) {
      const courseTypeItem = {
        value: item.courseType,
        label: mapCourseTypeLabel(item.courseType),
        courseType: item.courseType,
        children: [],
      };
      venue.children.push(courseTypeItem);
    }

    const courseTypeItem = venue.children.find(
      (child) => child.value === item.courseType
    );

    const courseItem = {
      value: item.courseId,
      label: item.courseName,
      courseId: item.courseId,
      courseName: item.courseName,
    };

    courseTypeItem.children.push(courseItem);
  });
  return targetData;
}

// Example usage
const originalJson = [
  {
    venueId: "1001",
    venueName: "王府井店",
    courseId: "1003",
    courseName: "私人培训课1",
    courseType: "1",
  },
  {
    venueId: "1001",
    venueName: "王府井店",
    courseId: "1004",
    courseName: "私人培训课2",
    courseType: "2",
  },
  {
    venueId: "1002",
    venueName: "天安门店",
    courseId: "100003",
    courseName: "团体培训课1",
    courseType: "3",
  },
  {
    venueId: "1002",
    venueName: "天安门店",
    courseId: "100004",
    courseName: "团体培训课2",
    courseType: "1",
  },
  {
    venueId: "1003",
    venueName: "天安门店1",
    courseId: "100004",
    courseName: "团体培训课2",
    courseType: "3",
  },
  {
    venueId: "1003",
    venueName: "天安门店1",
    courseId: "1000042",
    courseName: "团体培训课21",
    courseType: "2",
  },
];

const transformedJson = transformData(originalJson);
// console.log(JSON.stringify(transformedJson, null, 2));

export default transformData;
